using FlowCanvas.Nodes;
using UnityEngine;
using Yoozoo.Gameplay;
using Yoozoo.Gameplay.RTS;
using Yoozoo.Gameplay.RTS.Proto;

namespace com.yoozoo.gta.Gameplay.RTS
{
    public class RtsServerInfo 
    {
        public long tid;
        public TargetCoordinateType Type;
        public MarchStatus marchStatus;
        public MarchType marchType;
        public int team;
        public bool DefenceTroop = false;//MapCommonBattleObj 类型的不会动的部队
            
        public float radius;
        public long playerId;
        public string playerName;
        public long buildingUid;
        public TargetCoordinateType buildType; // 当前依赖建筑类型
        public int targetTeam;
        public bool isAssemblyLeader;
        public bool isShowAssemblyHeadFrame;
        public long assemblyId;
        public long leavePveAssemblyId;
        public string name;
        public long leaderTroopId;//车头部队id

        public bool isNewFromServer;
        public long troopWorldId;// 当前部队所在服务器id >0 为有效值
        public bool isNullTroop;
        #region 渲染相关数据

        public int hero1;
        public int hero2;
        public int hero3;
        public int SoldierType;
        public LuaArrAccess soldierListAccess;
        public int soldierCount;
        public int initSoldierCount;
        public int SoldierListLength;
        public bool visible;
        public int curHeroIdx;

        public long expireMills;
        //只会初始化一次的
        public int[] heroQualityList;
        public string[] heroIconList;
        public int[] heroRageList;
        public int MaxRage;
        public int formationId;//阵型ID 野怪专用
        public bool needReloadFormation;
        public bool isLoadLod1Hud = true;
        public string modelName;
        public long alliandeId;
        #endregion
        #region 移动数据

        public Coordinate coordinate;
        public Coordinate[] pathList;
        public long lastFrameTime;
        public float speed;
        public int attRange;
        public CrossingGate crossDetail;
        
        public int CampId = -1; // 阵营id KVK对决用 <=0 代表无效值 TeamFriend代表同阵营 TeamEnemy代表不同阵营 TeamRedName代表同阵营中的红名
      
        #endregion
        #region 行军目标，战斗目标数据

        public long targetId;
        public TargetCoordinateType targetType;

        public long attackTargetId;
        public TargetCoordinateType attackTargetType;

        public Vector3 targetPosition;
        public bool isFightConditionNotMet;

        #endregion

        #region 夹击数据

        public int attackTargetUnitType;
        /// <summary>
        /* 夹击移动的最终角度 当值为-2.0时 客户端需要预判部队的夹击位置 平滑夹击移动
     -2.0: 表示部队还未寻找夹击角度 默认值/取消对目标的夹击时赋值为-2.0
     -1.0: 表示部队夹击移动结束 已移动到夹击位
      >0: 表示部队在夹击移动中 value表示夹击移动的最终角度*/
        /// </summary>
        public double besiegeFinalAngle;

        //夹击用，lua传过来的
        //夹击的圆心
        public Vector3 besiegeCenter;

        //夹击的半径
        public float besiegeRadius;

        public long attackTargetBuildingId;
        public double garrisonAngle;
        #endregion

        #region 野怪专用数据

        public RtsNpcType npcType;
        public int radarEventQuality;
        public int level;
        public string heroBgIcon;
        public string radarIcon;
        public int radarEventType;
        public int radarType;
        public string radarEventRewardIcon;
        public string radarEquipType;
        public int npcConfigType;

        public int truckColor;
        public int truckBeGrabbedTimes;

        public int trainBeGrabbedTimes;
        
        #endregion

        #region 集结部队

        public string LogoBgColor;
        public string LogoBg;
        public string LogoColor;
        public string Logo;
        public string LogoFrame;
        public int AssemblyIconType;

        #endregion
        
        #region 玩家外观表现

        public string roleTitle;
        public int marchSkinResId;
        #endregion
        
        public void OnReset()
        {
            tid = default;
            Type = 0;
            marchStatus = 0;
            marchType = 0;
            team = -1;
            alliandeId = 0;
            modelName = string.Empty;
            DefenceTroop = false;
            playerId = default;
            buildingUid = default;
            targetTeam = default;
            isAssemblyLeader = default;
            assemblyId = default;
            name = string.Empty;
            leaderTroopId = default;

            hero1 = 0;
            hero2 = 0;
            hero3 = 0;
            SoldierType = 0;
            soldierListAccess = null;
            soldierCount = 0;
            initSoldierCount = 0;
            SoldierListLength = 0;
            visible = false;

            heroQualityList = null;
            heroIconList = null;
            heroRageList = null;
            MaxRage = 0;
            formationId = 0;
            isLoadLod1Hud = true;
            coordinate = default;
            pathList = null;
            lastFrameTime = default;
            speed = 0;
            attRange = 0;

            targetId = 0;
            targetType = 0;
            curHeroIdx = 0;
            
            attackTargetId = 0;
            attackTargetType = 0;

            targetPosition = default;
            isFightConditionNotMet = false;

            attackTargetUnitType = 0;
            besiegeFinalAngle = -2;

            attackTargetBuildingId = 0;
            garrisonAngle = 0;
            npcType = 0;
            radarEventQuality = 0;
            level = 0;
            heroBgIcon = string.Empty;
            radarIcon = string.Empty;
            radarEventType = 0;
            radarType = 0;
            radarEventRewardIcon = string.Empty;
            radarEquipType = string.Empty;
            
            Logo = string.Empty;
            LogoBg = string.Empty;
            LogoFrame = string.Empty;
            LogoColor = string.Empty;
            LogoBgColor = string.Empty;
            needReloadFormation = false;
            roleTitle = string.Empty;
            marchSkinResId = 0;
            leavePveAssemblyId = 0;
            buildType = default;

            truckColor = 0;
            truckBeGrabbedTimes = 0;

            trainBeGrabbedTimes = 0;

            CampId = 0;
        }

        public bool IsCrossingGate
        {
            get
            {
                if (crossDetail.startTime == 0 || crossDetail.startTime + BattleUtils.CrossingGateTime * 1000 < TimeUtils.GetServerTimeMs())
                {
                    return false;
                }
                return true;
            }
        }

        public bool IsWonderNpc()
        {
            return npcType == RtsNpcType.NPC_MIRACLE_GUARD || npcType == RtsNpcType.NPC_MIRACLE_MOBILIZATION ||
                   npcType == RtsNpcType.NPC_MIRACLE_REINFORCE;
        }
        public bool IsSumggler()
        {
            return npcType == RtsNpcType.NPC_SMUGGLER;
        }
        public bool IsPracticeBoss()
        {
            return npcType == RtsNpcType.NPC_PRACTICE_BOSS;
        }
        public bool IsWorldBoss()
        {
            return npcType == RtsNpcType.NPC_WORLD_BOSS;
        }

        public bool IsInAliinaceBuild()
        {
            if (buildType == null) return false;
            return buildType == TargetCoordinateType.ALLIANCE_CASTLE_10 ||
                   buildType == TargetCoordinateType.ALLIANCE_FLAG_11;
        }

        public bool IsInWonder()
        {
            if (buildType == null) return false;
            return buildType == TargetCoordinateType.MIRACLE_CASTLE_14;
        }
        
        public int GetIdxInPathList(Coordinate pos)
        {
            int idx = 0;
           
            Vector3 vector1 = new Vector3();
            Vector3 vector2 = new Vector3();
            for (int i = 0; i < pathList.Length; i++)
            {
                if (i < pathList.Length-1)
                {
                    Coordinate _start = pathList[i];
                    Coordinate _end = pathList[i + 1];
                    vector1.x = _end.xAxis - _start.xAxis;
                    vector1.z = _end.zAxis - _start.zAxis;

                    //// 向量1: 起点到终点
                    vector2.x = pos.xAxis - _start.xAxis;
                    //// 向量2: 起点到当前点
                    vector2.z = pos.zAxis - _start.zAxis;

                    // 向量点积
                    var dotProduct = vector1.x * vector2.x + vector1.y * vector2.y;
                    // 向量1的长度乘以向量2的长度的乘积
                    var lengthProduct = Mathf.Sqrt(vector1.x * vector1.x + vector1.y * vector1.y) *
                                        Mathf.Sqrt(vector2.x * vector2.x + vector2.y * vector2.y);

                    // 如果点积除以长度乘积接近1，则点在直线上
                    var cosine = dotProduct / lengthProduct;
                    if (Mathf.Abs(cosine - 1) < 1e-10)
                    {
                        // 再判断长度
                        if (Vector3.Distance(pos.ToVector3(), _start.ToVector3()) < Vector3.Distance(_end.ToVector3(), _start.ToVector3()))
                        {
                            idx = i;
                            break;
                        }
                    }
                }
               
            }
            return idx;
        }
    }
}
